Skip to content

feat: add Span<T> params to C# struct Create methods for zero-allocation stackalloc support#8970

Open
statxc wants to merge 3 commits intogoogle:masterfrom
statxc:csharp/span-params-create-struct
Open

feat: add Span<T> params to C# struct Create methods for zero-allocation stackalloc support#8970
statxc wants to merge 3 commits intogoogle:masterfrom
statxc:csharp/span-params-create-struct

Conversation

@statxc
Copy link
Contributor

@statxc statxc commented Mar 11, 2026

Summary

  • Add Span<T> parameters to C# struct CreateXXX methods under #if ENABLE_SPAN_T, enabling stackalloc for zero-GC struct creation
  • Flatten multi-dimensional array parameters (T[,]) to 1D Span<T> with computed indices for nested struct arrays
  • Backward compatible: existing code passing T[] still compiles since arrays implicitly convert to Span<T>

Changes

  • src/idl_gen_csharp.cpp: Add Span<T> codegen path with StructHasArrayFields() helper and flat indexing support
  • tests/MyGame/Example/{NestedStruct,ArrayStruct,LargeArrayStruct}.cs: Regenerated via flatc
  • tests/FlatBuffers.Test/FlatBuffersExampleTests.cs: Updated test for flat array params under ENABLE_SPAN_T
  • tests/FlatBuffers.Test/FlatBuffersFixedLengthArrayTests.cs: Added stackalloc test demonstrating zero-allocation usage

How to test

# 1. Build flatc
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -B build && cmake --build build --target flatc -j

# 2. Regenerate C# files
cd tests && ../build/flatc --csharp --gen-object-api --cs-gen-json-serializer --gen-mutable arrays_test.fbs && cd ..

# 3. Run tests (default)
dotnet test tests/FlatBuffers.Test/FlatBuffers.Test.csproj

# 4. Run tests (unsafe byte buffer)
dotnet test tests/FlatBuffers.Test/FlatBuffers.Test.csproj -p:DefineConstants=UNSAFE_BYTEBUFFER

# 5. Run tests (Span<T> + unsafe byte buffer)
dotnet test tests/FlatBuffers.Test/FlatBuffers.Test.csproj -p:DefineConstants="ENABLE_SPAN_T%3BUNSAFE_BYTEBUFFER"

Test plan

  • Default build: 156 tests passed
  • UNSAFE_BYTEBUFFER=true: 156 tests passed
  • ENABLE_SPAN_T=true + UNSAFE_BYTEBUFFER=true: 156 tests passed
  • Regenerated C# files match flatc output (zero diff)

Closes #8927

@statxc statxc requested a review from dbaileychess as a code owner March 11, 2026 13:24
@github-actions github-actions bot added c# c++ codegen Involving generating code from schema labels Mar 11, 2026
@statxc
Copy link
Contributor Author

statxc commented Mar 11, 2026

@jtdavis777 Could you please review this PR? I'd appreciate any feedbacks. Thanks!

@ODtian
Copy link

ODtian commented Mar 11, 2026

@statxc In practice, it is better to use ReadOnlySpan<T> as the parameter type when read-only access is intended, as it enforces the principle of least privilege. BTW, there is no need to provide a separate Span<T> overload because Span<T> implicitly converts to ReadOnlySpan<T>

@statxc
Copy link
Contributor Author

statxc commented Mar 11, 2026

@statxc In practice, it is better to use ReadOnlySpan<T> as the parameter type when read-only access is intended, as it enforces the principle of least privilege. BTW, there is no need to provide a separate Span<T> overload because Span<T> implicitly converts to ReadOnlySpan<T>

Yes. Good point! 100% right. Our Create methods only read from the array parameters - they never write to them. So the parameter type should be ReadOnlySpan instead of Span. Thanks @ODtian 👍. I will update soon

@statxc
Copy link
Contributor Author

statxc commented Mar 11, 2026

@ODtian I updated all! Could you review again? thanks

@ODtian
Copy link

ODtian commented Mar 12, 2026

@statxc The rest looks good to me. I'm not very familiar with FlatBuffers itself though, so it'd be best to have someone with more expertise take a look.

@statxc
Copy link
Contributor Author

statxc commented Mar 12, 2026

@jtdavis777 could you please review this PR? Welcome to any feedbacks

@statxc
Copy link
Contributor Author

statxc commented Mar 17, 2026

@jtdavis777 Sorry for ping again. Any updates for me, please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c# c++ codegen Involving generating code from schema

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Span params for CreateXXX in C#

2 participants